package com.dyj.ads

import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.sql.expressions.Window

object ads_e_mz_au5800_gbzam {

  def main(args: Array[String]): Unit = {
    val ds: String = args(0)

    val sparkSession: SparkSession = SparkSession.builder()
      .appName("谷丙转氨酶指标统计")
      .enableHiveSupport()
      .config("spark.sql.shuffle.partitions", 1)
      .getOrCreate()

    import sparkSession.implicits._
    import org.apache.spark.sql.functions._

    sparkSession.sql("use bigdata03_dws")

    val gbzamDF: DataFrame = sparkSession.sql(
      s"""
        |select
        |baoGaoBianHao,
        |guBingZhuanAnMei,
        |baoGaoRiQi,
        |gbzam_max,
        |gbzam_min,
        |gbzam_avg
        |from
        |bigdata03_dws.dws_e_mz_au5800shenghua
        |where
        |ds='${ds}'
        |""".stripMargin)

    gbzamDF
      .withColumn("number", count($"baoGaoBianHao") over Window.partitionBy($"baoGaoBianHao"))
      .withColumn("e_fc", ($"guBingZhuanAnMei" - $"gbzam_avg") * ($"guBingZhuanAnMei" - $"gbzam_avg"))
      .withColumn("a_fc", sum($"e_fc").over())
      .withColumn("gbzam_fc", $"a_fc" / $"number")
      .withColumn("gbzam_bzc", sqrt($"gbzam_fc"))
      .withColumn("gbzam_ycgs", sum(when($"guBingZhuanAnMei" > 40, 1)).over())
      .select($"baoGaoBianHao", $"guBingZhuanAnMei", $"baoGaoRiQi", $"gbzam_max", $"gbzam_min", $"gbzam_avg", $"gbzam_fc", $"gbzam_bzc", $"gbzam_ycgs")
      .write
      .format("csv")
      .save("/daas/motl/bigdata03/ads/ads_e_mz_au5800_gbzam/ds=" + ds)
  }

}
